package net.kesumu.general.android.opengl.object;

import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.program.TextureShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuBackground {

    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT 
        + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public final int texture;
    
    private final VertexArray vertexArray;
    
    public YushabuBackground(int texture,Point mostLeftPoint, float width, float height) {
        this.texture = texture;
        final float[] VERTEX_DATA = {
                // Order of coordinates: X, Y, S, T

                // Triangle Fan
            mostLeftPoint.x + 0.5f * width,  mostLeftPoint.y + 0.5f * height, 0.5f, 0.5f, 
            mostLeftPoint.x,    mostLeftPoint.y,   0f,   1f,  
            mostLeftPoint.x + width, mostLeftPoint.y,   1f,   1f, 
            mostLeftPoint.x + width,  mostLeftPoint.y + height,   1f,   0f, 
            mostLeftPoint.x,  mostLeftPoint.y + height,   0f,   0f, 
            mostLeftPoint.x,  mostLeftPoint.y,   0f,   1f};
        
        vertexArray = new VertexArray(VERTEX_DATA);
    }
    
    public void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
        
    public void draw() {                                
        glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    }    
}
